package View.PayTheFeet;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import View.Index.Index_user;
import domain.Dorm;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class PayTheFeets extends JFrame {

    public PayTheFeets(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("缴费");
        this.setSize(539,400);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("请填写缴费信息");
        label1.setBounds(183,3,137,61);
        JLabel label2= new JLabel();
        label2.setText("宿舍号:");
        label2.setBounds(150,82,65,30);
        JLabel label3= new JLabel();
        label3.setText("缴费类型:");
        label3.setBounds(149,135,65,30);
        JLabel label4= new JLabel();
        label4.setText("缴费金额:");
        label4.setBounds(150,184,65,30);

        //------------------JTextField--------------------
        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(236,82,120,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(235,184,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("缴费");
        button1.setBounds(124,260,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(278,256,95,31);

        String[] item = new String[3];
        item[0]="请选择缴费类型:";
        item[1]="电费";
        item[2]="水费";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(235,135,120,30);



        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(comboBox2);
        this.add(field2);
        this.add(button1);
        this.add(button2);
        this.add(comboBox1);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String dormnum = (String) comboBox2.getSelectedItem();
                String Type = (String) comboBox1.getSelectedItem();
                Double money = Double.valueOf(field2.getText());
                System.out.println("text2:"+money);
                DormService service = new DormServiceImpl();
                if (Type.equals("水费")){
                    Dorm dorm = service.selectDormByDormnum(dormnum);

                    double water = dorm.getWater();
                    System.out.println("water"+water);
                    money = water+money;
                    System.out.println("水费:"+money);
                    service.payWater(Double.valueOf(money),dormnum);
                    dispose();
                    new successPage("水费",Double.valueOf(field2.getText()),dormnum,user).setVisible(true);
                }

                if(Type.equals("电费")){
                    Dorm dorm = service.selectDormByDormnum(dormnum);
                    double ele = dorm.getEle();
                    System.out.println("ele:"+ele);
                    money=ele+money;
                    System.out.println("电费:"+money);
                    service.payEle(Double.valueOf(money),dormnum);
                    dispose();
                    new successPage("电费",Double.valueOf(field2.getText()),dormnum,user).setVisible(true);
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_user(user).setVisible(true);
            }
        });
    }
}
